/*
 * @Author: sunzhijun 
 * @Date: 2020-02-28 21:36:16 
 * @Last Modified by: sunzhijun
 * @Last Modified time: 2020-04-01 10:09:47
 */

import CommonAction from './common';
import newIncreasedRefs from '../components/NewAdd/functions';
import { chectFormAction, setFormAttrAndValue } from '../../../public/functions/checkFormAction';
import { saveValidate } from 'src/hrpub/common/utils/utils';

export default class newadd extends CommonAction {
    constructor(comp){
        super();
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    // 上一步操作
    prevHandle = ()=>{
        // newIncreasedRefs.refs.prevHandleFn();
        this.cancelOccupy()
    }

    // 下一步操作 
    nextHandle = async ( selectData )=>{
        const { props } = this.comp;
        let { main, meta, form } = props;
        let { appConfig, pk_org, language, areaCode, popCardId} = main;
        // debugger
        let pk_psnjob = [];
        selectData && selectData.map((v,i)=>{
            pk_psnjob.push(v.data.values.pk_psnjob.value);
        });
        let psnJobs = pk_psnjob;
        try {
            let res = await this.dispatch({
                type: 'main/nextHandleAction',
                payload: {
                    pageCode: appConfig.pagecode,
                    pk_psnjobs: psnJobs,
                    areaCode: areaCode,
                    appCode: appConfig.appcode,
                    pk_org: pk_org
                }
            });
            if( res.success ){
                // let tempCardid = 'dataCard_add';
					// tempAgreementid = 'showAgreement_add';
                let allMeta = meta.getMeta();
                let rows = res.data.resultData[popCardId].rows;
                // debugger
                await this.dispatch({
                    type: 'main/update',
                    payload: {
                        formVisible: true,
                        rollBackFlag: res.data.rollBackFlag,
                        rollBackFlagBillCode: res.data.rollBackFlagBillCode
                    }
                })
				allMeta[popCardId] = res.data.template[popCardId];
				// allMeta[tempAgreementid] = res.data.template[tempAgreementid];
				meta.addMeta(allMeta);
				// meta.setMeta(allMeta, () => {
					// chectFormAction('', tempCardid, this.props, [ tempCardid ], appcode);
                form.setAllFormValue({
                    [popCardId]: { rows: rows }
                });
                // });
                form.setFormStatus(popCardId, 'edit');
                
            }
        }catch (err) {
            this.prevHandle();
            // this.toast({
            //     color: 'danger',
            //     content: err.message
            // })
            throw err
        }
    }

    // 保存
    doneHandle = ( )=>{
        const { props, action } = this.comp;
        let { main, form } = props;
        let { appConfig: { appcode, pagecode }, pk_org, step, editColumn, popCardId, pageid, isCardForm, language } = main;
        if (!form.isCheckNow(popCardId)) {
            return false
        }
        saveValidate(props, pageid, popCardId, null, 'form').then( async ()=>{
            let formDataRows = form.getAllFormValue(popCardId).rows
            let formData = {
                model: {
                    rows: formDataRows
                }
            }
            let paramObj = {
                step: step,
                pk_org: pk_org,
                appCode: appcode,
                modename: 'hrcm_ctrtmng',
                formData: formData,
                editColumn: editColumn,
            }
            let res = await this.dispatch({
                type: 'main/newAddSaveAction',
                payload: {
                    ...paramObj
                }
            })
            if( res.success ){
                // debugger;
                let data = res.data;
                this.dispatch({
                    type: 'main/update',
                    payload: {
                        step: data.step,
                        isDone: true
                    }
                })
                let title = language['hrcm-000041'] /*国际化：确认继续*/; 
                if (data.step !== '99') {
                    this.addSaveAfterTips(data.msg, title)
                } else {
                    let savedData = data.savedData;
                    this.toast({
                        color: 'success',
                        content: language['hrcm-000045'] // 保存成功
                    })
                    await this.dispatch({
                        type: 'main/update',
                        payload: {
                            savedData: savedData,
                            step: '0',
                            pk_ctrtmng: savedData && savedData[0],
                            cardPaginationStatus: 2
                        }
                    })
                    await action.tableAct.getTableData();
                    if( isCardForm ){
                        action.cardAct.getFormData();
                    }
                    this.onClose();
                }
            }
        })

    }

    // 点击保存之后的提示
    addSaveAfterTips = (message, title)=> {
        const { props } = this.comp;
        let { main: { language }} = props;
        this.promptBox({
            color: 'warning',
            title: title, // 确认继续
            content: message,
            noFooter: false,
            noCancelBtn: false,
            beSureBtnName: language['hrcm-000034'], // 是
            cancelBtnName: language['hrcm-000035'], // 否
            beSureBtnClick: this.doneHandle,
            hasCloseBtn: false,
            cancelBtnClick: this.cancelBtnClick
        })
    }
    // 取消解除合同
    cancelBtnClick = async ()=>{
        this.dispatch({
            type: 'main/update',
            payload: {
                step: '0'
            }
        })
    }
    // 关闭新增弹窗
    onClose = ()=>{
        const { props: {main:{isDone}} } = this.comp;
        !isDone && this.cancelOccupy();
        this.dispatch({
            type: 'main/update',
            payload: {
                setModalVisible: false,
                isDone: false
            }
        })
    }

    // form编辑前事件
    handleEditBefore = (props, moduleId, key, value,data)=>{
        const { main } = props;
        let { pk_org } = main;
        let meta = props.meta.getMeta();
        meta[moduleId].items.forEach((item) => {
            if (item.attrcode === key && item.itemtype === 'refer') {
                if (!item.queryCondition) item.queryCondition = {}
                Object.assign(item.queryCondition, {
                    pk_org: pk_org
                })
            }
        });
        props.meta.setMeta(meta);
        return true;
    }

    // form 编辑后事件
    onAfterEvent = (props, moduleId, key, value, oldValue)=>{
        // debugger
        const { main } = props;
        let { appConfig:{ appcode }} = main;
        let url = '/nccloud/hrcm/ctrtmng/CtrtMngFormAfterEditAction.do';
        chectFormAction(key, moduleId, props, [moduleId], appcode, '', url)
    }
    //取消下一步人员数据被占用
    async cancelOccupy(){
        const { props } = this.comp;
        let {main:{pk_org, rollBackFlag, rollBackFlagBillCode}} = props;
        let formCardId = 'dataCard_add';
        if( rollBackFlag || rollBackFlagBillCode){
            let occupyData = props.form.getAllFormValue(formCardId);
            let occupyValues = occupyData.rows[0].values;
            let billCode = occupyData && occupyValues.bill_code.value;
            let res = await this.dispatch({
                type: 'main/rollBackAction',
                payload: {
                    codeList: [], // 只有签订申请有合同编号回滚
                    pk_org: pk_org,
                    billCode: rollBackFlagBillCode ? billCode: '',
                    rollBackFlagBillCode: rollBackFlagBillCode,
                    rollBackFlag: rollBackFlag
                }
            })

            if (res.success) {
                /*清空表单所有数据*/
                props.form.EmptyAllFormValue(formCardId);
            }
        }
    }
 }